//
//  SRAddClassViewController.m
//  StoryShow
//
//  Created by CyonLeuMBP on 2017/7/20.
//  Copyright © 2017年 qudiandu.cn. All rights reserved.
//

#import "SRAddClassViewController.h"
#import <NSString+Enhanced.h>
#import "FCUtil.h"
#import "SRSchoolClassServices.h"
#import "SRClassConstants.h"

@interface SRAddClassViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *schoolNameTextField;
//@property (weak, nonatomic) IBOutlet UITextField *gradeTextField;
//@property (weak, nonatomic) IBOutlet UITextField *classNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (strong, nonatomic) UIButton *rightButton;

@end

@implementation SRAddClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    self.title = @"我的班级";
    
    [self setupView];
    
    [self setupNavigationItem];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:self.schoolNameTextField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:self.phoneTextField];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:self.gradeTextField];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:self.classNameTextField];
    
    WEAKSELF
    [self.view addTapGestureHandler:^(UITapGestureRecognizer *gestureRecognizer, UIView *view, CGPoint locationInView) {
        [weakSelf.view endEditing:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
}


- (void)setupView {
    
    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    self.view.backgroundColor = css.color_2;
    
    self.schoolNameTextField.textColor = self.phoneTextField.textColor = [UIColor colorWithHex:0x29221c];
    
    self.schoolNameTextField.font = self.phoneTextField.font = css.fontOfH6;
    self.schoolNameTextField.delegate = self.phoneTextField.delegate = self;
    
    
}


- (void)setupNavigationItem {
    
    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 80, 40);
    [rightButton setTitle:@"加入班级" forState:UIControlStateNormal];
    rightButton.titleLabel.font = css.fontOfH5;
    
    self.rightButton = rightButton;
    [rightButton addTarget:self action:@selector(onRightButton:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    [self setRightBarButtonItem:rightItem];
}


#pragma mark - Event


- (void)onRightButton:(id)sender {
    WEAKSELF
    [self hideKeyboard];
    
    NSString *schoolName = [self schoolName];
    if (!schoolName || [schoolName length] == 0) {
        [self showHUDErrorMessage:@"请输入真实姓名"];
        return;
    }
    
    NSString *phone = [self phone];
    if (!phone || [phone length] == 0) {
        [self showHUDErrorMessage:@"请输入班级邀请码"];
        return;
    }
    
    [weakSelf startProgressHUD];
    
    [SRSchoolClassServices addClassWithNickname:schoolName code:phone successBlock:^(NSInteger statusCode, NSString *message, id responseDataObject) {
        [weakSelf stopProgressHUD];
        if (statusCode == kFZRequestStatusCodeSuccess) {
            [weakSelf showHUDHintWithText:@"加入成功"];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:kSRAddClassDidSuccessNotification object:nil];
            
            [weakSelf performSelector:@selector(doBack) withObject:nil afterDelay:1];
        } else {
            [weakSelf showHUDHintWithText:message];
        }
    } failureBlock:^(id responseObject, NSError *error) {
        [weakSelf stopProgressHUD];
        [weakSelf showHUDError];
    }];
}

- (void)hideKeyboard {
    [self.schoolNameTextField resignFirstResponder];
    [self.phoneTextField resignFirstResponder];
//    [self.gradeTextField resignFirstResponder];
//    [self.classNameTextField resignFirstResponder];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.schoolNameTextField) {
        [self.phoneTextField becomeFirstResponder];
    }
    else {
        [self.phoneTextField resignFirstResponder];
        [self onRightButton:nil];
    }
    
    return YES;
}

- (NSString *)schoolName {
    return [self.schoolNameTextField.text stringToTrimWhiteSpace];
}

- (NSString *)phone {
    return [self.phoneTextField.text stringToTrimWhiteSpace];
}

- (void)textViewDidChangeNotification:(NSNotification *)notification {
    if (notification.object) {
        [self termTextForTextfield:notification.object maxLength:30];
    }
}


- (void)termTextForTextfield:(UITextField *)textField maxLength:(NSInteger)maxLength {
        //    NSInteger maxLength = 20;
    NSArray *lengths = [FCUtil stringLengthWithString:textField.text maxLength:maxLength];
    if([[lengths objectAtIndex:0] integerValue] > maxLength){
        NSString * text = [textField.text substringToIndex:[[lengths objectAtIndex:1] integerValue]];
        if([NSThread isMainThread]){
            textField.text = text;
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                textField.text = text;
            });
        }
            //        [self showHUDErrorMessage:@"已超出字符限制"];
        NSString *message = [NSString stringWithFormat:@"最大允许 %ld 字符", (long)maxLength];
        [self showHUDErrorMessage:message];
        
            //        if (self.showMessageBlock) {
            //            self.showMessageBlock(message);
            //        }
    }
}

@end
